import tkinter as tk
from tkinter import ttk
import studentsdb


class StudentsInputFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master
        self.StudentsInput_window = tk.Toplevel(self.master)
        self.StudentsInput_window.title('学生信息录入')
        self.StudentsInput_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.StudentsInput_window)
        self.page.pack()
        self.sno = tk.StringVar()
        self.sname = tk.StringVar()
        self.subject = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='学号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='学生名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='专业: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.subject).grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='录入学生信息', command=self.record_StudentsInput).grid(row=4, column=1, sticky=tk.E,
                                                                                       pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def record_StudentsInput(self):
        sno = self.sno.get()
        sname = self.sname.get()
        subject = self.subject.get()

        if not sno:
            self.status.set('录入失败，学号不能为空')
            return

        if studentsdb.students_exists(sno):
            self.status.set('录入失败，学号已存在')
            return

        studentsdb.insert_students(sno, sname, subject)
        self.status.set('学生信息录入成功')



class StudentsQueryFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master
        self.StudentsQuery_window = tk.Toplevel(self.root)
        self.StudentsQuery_window.title('学生信息查询')
        self.StudentsQuery_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.StudentsQuery_window)
        self.page.pack()
        self.sno = tk.StringVar()
        self.sname = tk.StringVar()
        self.subject = tk.StringVar()
        self.status = tk.StringVar()  # 确保初始化 status 变量
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='学号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='学生名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sname, state='readonly').grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='专业: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.subject, state='readonly').grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='查询学生信息', command=self.query_StudentsInfo).grid(row=4, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def query_StudentsInfo(self):
        sno = self.sno.get()
        result = studentsdb.query_by_sno(sno)
        if result:
            self.sname.set(result['sname'])
            self.subject.set(result['subject'])
            self.status.set('查询成功')
        else:
            self.sname.set('')  # 如果没有找到信息，清空学生名和专业输入框
            self.subject.set('')
            self.status.set('没有找到对应的学生信息')


class StudentsChangeFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.StudentsChange_window = tk.Toplevel(self.master)
        self.StudentsChange_window.title('学生信息修改')
        self.StudentsChange_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.StudentsChange_window)
        self.page.pack()
        self.sno = tk.StringVar()
        self.sname = tk.StringVar()
        self.subject = tk.StringVar()
        self.status = tk.StringVar()  # 确保初始化 status 变量
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='学号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='学生名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='专业: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.subject).grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='修改学生信息', command=self.change_StudentsInfo).grid(row=4, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def change_StudentsInfo(self):
        sno = self.sno.get()
        data = {
            'sname': self.sname.get(),
            'subject': self.subject.get()
        }
        result = studentsdb.query_by_sno(sno)
        if result:
            studentsdb.update_by_sno(sno, data)
            self.status.set('修改成功')
        else:
            self.status.set('没有找到对应的学生信息')
